﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ImageUploader.domain;
using System.IO;
using System.DirectoryServices;

namespace ImageUploader.GUI
{
    public partial class ImportUsersForm : Form
    {
        private DomainController domainController;


        public ImportUsersForm(DomainController dc)
        {
            domainController = dc;
            InitializeComponent();
            cboImageType.DataSource=(Enum.GetValues(typeof(ImportUsers.ImageFormat)));
            txtDistinguishedName.Text = "CN=Users";
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void ImportUsers_Load(object sender, EventArgs e)
        {

        }

        private void btnSelectSCV_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = false;
            ofd.Filter = "csv files (*.csv)|*.csv"; //|All files (*.*)|*.*
            ofd.ShowDialog();
            txtCSV.Text = ofd.FileName;
        }

        private void btnStartImport_Click(object sender, EventArgs e)
        {
            lblImporting.Visible = true;
            progressBar1.Visible = true;
            this.Enabled = false;
            this.Update();
            DirectoryEntry usersEntry;
            try
            {
                if (!File.Exists(txtCSV.Text) || new FileInfo(txtCSV.Text).Extension != ".csv")
                    throw new Exception("Invalid CSV file.");

                using (usersEntry = domainController.LDAPConnection.CreateDirectoryEntryWithDomainName(txtDistinguishedName.Text))//.Children.Find("CN=Users,DC=infodatek,DC=domein,DC=com");
                {
                    DirectorySearcher ds = new DirectorySearcher(usersEntry);
                    try
                    {
                        SearchResult sr = ds.FindOne();
                    }
                    catch (Exception)
                    {
                        throw new Exception("Invalid Distinguished Name!");
                    }

                    //domainController.ImportUsersFromCSV(txtCSV.Text, usersEntry);
                    ImportUsers iu = new ImportUsers(txtCSV.Text, /*domainController.LDAPConnection,*/ usersEntry,(ImportUsers.ImageFormat)cboImageType.SelectedValue,domainController);
                    iu.ProgressChanged += new Delegates.ProgressHandler(iu_ProgressChanged);
                    iu.Import();
                }
                MessageBox.Show("Successfully imported all users!");
                this.Close();
            }
            catch (AbortedException)
            {
                MessageBox.Show("Aborted.");
                this.Close();
            }
            catch (Exception ex)
            {
                this.Enabled = true;
                lblImporting.Visible = false;
                progressBar1.Visible = false;
                MessageBox.Show(ex.Message);
            }
        }

        private void iu_ProgressChanged(object sender, ProgressEventArgs pea)
        {
            progressBar1.Value = pea.Percentage;
            Application.DoEvents();
        }


    }
}
